package com.bhs.sansonglogistics.ui.enterprise;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.DedicatedLineBean;
import com.bhs.sansonglogistics.bean.DedicatedLineDataBean;
import com.bhs.sansonglogistics.bean.LinePromo;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.DedicatedLineAdapter;
import com.bhs.sansonglogistics.ui.home.OrderGoodsListFragment;
import com.bhs.sansonglogistics.ui.quality.PrivateLinePromotionActivity;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnConfirmListener;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 专线管理
 */
public class DedicatedLineManagementActivity extends BaseActivity implements NetCallBack, View.OnClickListener {


    private RecyclerView mRvList;
    private LinearLayout mLlAddLing;
    private int page = 1;

    private DedicatedLineAdapter mAdapter;
    private SwipeRefreshLayout refreshLayout;
    private Button mBtnBottomAdd;
    private String gold_switch, sliver_switch;
    private Button mBtnAddLine;
    private int type;
    private TextView mTvTitle;

    @Override
    public int initLayout() {
        return R.layout.activity_dedicated_line_management;
    }

    @Override
    public void initView() {
        mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("专线管理");
        mRvList = findViewById(R.id.rv_list);
        mLlAddLing = findViewById(R.id.ll_add_ling);
        mRvList.setVisibility(View.GONE);
        refreshLayout = findViewById(R.id.refreshLayout);
        mBtnBottomAdd = findViewById(R.id.btn_bottom_add);
        mBtnBottomAdd.setOnClickListener(this);
        mBtnAddLine = findViewById(R.id.btn_add_line);
        mBtnAddLine.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        page = 1;
        loadData();
        if (type == 2) {
            networkRequest(netApi.get_promote_config_info(), this, 888);
        }
    }

    @Override
    public void initData() {
        type = getIntent().getIntExtra("type", 1);
        if (type == 2) {
            mTvTitle.setText("专线推广");
        }
        mAdapter = new DedicatedLineAdapter(R.layout.item_dedicated_line, type);
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        mAdapter.disableLoadMoreIfNotFullPage(mRvList);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                DedicatedLineBean info = mAdapter.getData().get(position);
                mPosition = position;
                if (view.getId() == R.id.btn_sold_out) {
                    operation(info.getStatus() == 1 ? 0 : 1, info.getLine_id());
                } else if (view.getId() == R.id.btn_delete) {
                    operation(2, info.getLine_id());
                } else if (view.getId() == R.id.btn_range_transport) {
                    startActivity(new Intent(mActivity, RangeTransportActivity.class)
                            .putExtra("end_city", info.getEnd_city())
                            .putExtra("end_county", info.getEnd_county())
                            .putExtra("logistics_id", info.getLogistics_id())
                            .putExtra("end_province", info.getEnd_province()));
                } else if (view.getId() == R.id.btn_edit) {
                    startActivity(new Intent(DedicatedLineManagementActivity.this, AddLineActivity.class)
                            .putExtra("line_id", info.getLine_id()));
                } else if (view.getId() == R.id.btn_promotion) {
                    int promote_type = Integer.parseInt(info.getPromote_type());
                    if (gold_switch.equals("0") && promote_type > 0 && promote_type < 5) {
                        ToastUtil.show("充值通道关闭");
                        return;
                    }
                    if (gold_switch.equals("0") && sliver_switch.equals("0")) {
                        ToastUtil.show("充值通道关闭");
                        return;
                    }
                    startActivity(new Intent(DedicatedLineManagementActivity.this, PrivateLinePromotionActivity.class)
                            .putExtra("line_id", info.getLine_id())
                            .putExtra("expirationTime", info.getPromote_end_time())
                            .putExtra("promote_type", Integer.parseInt(info.getPromote_type())));
                }
            }
        });
    }

    private int mPosition;
    private int status;

    private void operation(int status, int id) {
        this.status = status;
        String hint = null;
        if (status == 0) {
            hint = "确定下架吗？";
        } else if (status == 1) {
            hint = "确定上架吗？";
        } else if (status == 2) {
            hint = "确定删除吗？";
        }
        new XPopup.Builder(mActivity)
                .asConfirm("提示", hint,
                        "取消", "确定",
                        new OnConfirmListener() {
                            @Override
                            public void onConfirm() {
                                if (status == 2) {
                                    networkRequest(netApi.delSpecialLine(id), DedicatedLineManagementActivity.this, 9528);
                                } else {
                                    networkRequest(netApi.editSpecialLineStatus(id, status), DedicatedLineManagementActivity.this, 9527);
                                }
                            }
                        }, null, false)
                .show();
    }

    private void loadData() {
        networkRequest(netApi.getSpecialList(page, 10), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 0) {
            DedicatedLineDataBean bean = new Gson().fromJson(json, DedicatedLineDataBean.class);
            refreshLayout.setRefreshing(false);
            if (bean.isStatus()) {
                if (page == 1) {
                    if (MyUtils.isEmpty(bean.getData().getList())) {
                        mLlAddLing.setVisibility(View.VISIBLE);
                        mRvList.setVisibility(View.GONE);
                        mBtnBottomAdd.setVisibility(View.GONE);
                    } else {
                        if (type == 1) {
                            mBtnBottomAdd.setVisibility(View.VISIBLE);
                        }
                        mRvList.setVisibility(View.VISIBLE);
                        mLlAddLing.setVisibility(View.GONE);
                        mAdapter.setNewData(bean.getData().getList());
                    }

                } else {
                    if (MyUtils.isEmpty(bean.getData().getList())) {
                        mAdapter.loadMoreEnd();
                    } else {
                        mAdapter.loadMoreComplete();
                    }
                    mAdapter.addData(bean.getData().getList());
                }

            } else {
                ToastUtil.show(bean.getMsg());
            }
        } else if (requestCode == 9527) {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            ToastUtil.show(bean.getMsg());
            if (bean.isStatus()) {
                mAdapter.getData().get(mPosition).setStatus(status);
                mAdapter.notifyItemChanged(mPosition);
            }
        } else if (requestCode == 9528) {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            ToastUtil.show(bean.getMsg());
            if (bean.isStatus()) {
                mAdapter.remove(mPosition);
            }
        } else if (requestCode == 888) {
            LinePromo linePromo = new Gson().fromJson(json, LinePromo.class);
            if (linePromo.isStatus()) {
                gold_switch = linePromo.getData().getLinePromote_gold_switch();
                sliver_switch = linePromo.getData().getLinePromote_silver_switch();
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void onClick(View view) {
        startActivity(new Intent(DedicatedLineManagementActivity.this, AddLineActivity.class));
    }
}